package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.MoviceBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.MoviceDao;
import com.jackiechancinema.ssm.dto.MoviceE;
import com.jackiechancinema.ssm.entity.Movices;
import com.jackiechancinema.ssm.listener.MoviceEx;
import com.jackiechancinema.ssm.service.Inte.MoviceService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class MoviceServiceImpl implements MoviceService {
    @Autowired
    private MoviceDao moviceDao;
    @Autowired
    private MoviceEx listener;


    //查询所有
    @Override
    public PageVo<Movices> findAll(int page,int limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Movices> pageInfo = new PageInfo<Movices>(moviceDao.queryAll());
//        return pageInfo;
        return new PageVo<Movices>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public PageVo<Movices> findAllV2(MoviceBo bo) {
        if (bo != null){
            //组装查询条件
            Movices movices = new Movices();
            if (StringUtils.hasLength(bo.getMovice_name())){
                movices.setMovice_name(bo.getMovice_name());
            }
            if (StringUtils.hasLength(bo.getMovice_actors())){
                movices.setMovice_actors(bo.getMovice_actors());
            }
            if (StringUtils.hasLength(bo.getMovice_director())){
                movices.setMovice_director(bo.getMovice_director());
            }
            if (bo.getMovice_type() > 0){
                movices.setMovice_type(bo.getMovice_type());
            }
            PageHelper.startPage(bo.getPage(),bo.getLimit());
            PageInfo<Movices> pageInfo = new PageInfo<>(moviceDao.queryAllV2(movices));
            return new PageVo<Movices>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());

        }
        return new PageVo<Movices>(SystemContant.PAGE_R,"OK",0 ,null);

    }

    //添加
    @Override
    public R MoviceAdd(Movices movices) {
        if (movices.getMovice_type() >= 1){
            int i = moviceDao.insertAdd(movices);
            if (i > 0){
                return R.ok(null);
            }
        }
        return R.fail("请选择电影类型");
    }

    //修改
    @Override
    public R MoviceUpdate(Movices movices) {
        int i = moviceDao.updateUpdate(movices);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    //根据ID删除
    @Override
    public R MoviceDel(int id) {
        int i = moviceDao.deleteDel(id);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R insertAddS(MultipartFile file) {
        System.out.println("bbb");
        if(!file.isEmpty()){
            System.out.println("ccc");
            //excel解析
            try {
                EasyExcel.read(file.getInputStream(), MoviceE.class,listener).sheet().doRead();
                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
